﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Maps.MapControl;
using UpYourLife.UserControls;
using UpYourLife.Helpers;
using UpYourLife.Extensions;
using UpYourLife.UpYourLifeServices;
using System.ServiceModel;

namespace UpYourLife.Views
{
    public partial class MapPage : PhoneApplicationPage
    {   
        UpYourLifeServicesClient proxy;        
        MapLayer pinLayer;
        Friend myProfile;
        Location myLocation;
        GoogleLocalResult result;
        
        public MapPage()
        {
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
            PopupFindPlaces.Visibility = Visibility.Collapsed;
            spFindAddress.Visibility = Visibility.Collapsed;
            this.Loaded += new RoutedEventHandler(MapPage_Loaded);
            gridFindAdvance.Visibility = Visibility.Collapsed;
        }

        void MapPage_Loaded(object sender, RoutedEventArgs e)
        {
            pinLayer = new MapLayer();
            GoogleMap.Children.Add(pinLayer);

            proxy = new UpYourLifeServicesClient(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/UpYourLife"));
            proxy.GetFriendByIDAsync(new Guid("b0aab6f6-393e-4fde-b92e-3be09beb32ea"));
            proxy.GetFriendByIDCompleted += new EventHandler<GetFriendByIDCompletedEventArgs>(proxy_GetFriendByIDCompleted);
        }

        void proxy_GetFriendByIDCompleted(object sender, GetFriendByIDCompletedEventArgs e)
        {
            myProfile = e.Result;
            myLocation = new Location(myProfile.Latitude, myProfile.Longitude);
        }        
        
        private void btnFindNearlyPlaces_Click(object sender, RoutedEventArgs e)
        {
            List<string> places = new List<string>();
            
            if(chkBookStore.IsChecked == true)
            {
                places.Add("BookShop");
            }
            if(chkCinema.IsChecked == true)
            {
                places.Add("Cinema");
            }
            if(chkCoffeeShop.IsChecked == true)
            {
                places.Add("CoffeeShop");
            }
            if(chkFoodDrink.IsChecked == true)
            {
                places.Add("FoodDrink");
            }
            if(chkShopping.IsChecked == true)
            {
                places.Add("Shopping");
            }

            places.Remove(null);

            proxy.GetPlacesByTypesAsync(places);
            proxy.GetPlacesByTypesCompleted += new EventHandler<GetPlacesByTypesCompletedEventArgs>(proxy_GetPlacesByTypesCompleted);
            //stbHidePopup.Completed += new EventHandler(stbHidePopup_Completed);
            //stbHidePopup.Begin();
        }

        void proxy_GetPlacesByTypesCompleted(object sender, GetPlacesByTypesCompletedEventArgs e)
        {
            List<Place> lstPlaces = e.Result;

            BuildPlacePushpin(lstPlaces);
            GoogleMap.ZoomLevel = 15;
            GoogleMap.Center = myLocation;
            stbHidePopup.Completed += new EventHandler(stbHidePopup_Completed);
            stbHidePopup.Begin();
        }

        void stbHidePopup_Completed(object sender, EventArgs e)
        {
            PopupFindPlaces.Visibility = Visibility.Collapsed;
            stbHidePopup.Completed -= stbHidePopup_Completed;
        }

        void BuildPlacePushpin(List<Place> lstPlaces)
        {
            pinLayer.Children.Clear();
            foreach (Place place in lstPlaces)
            {
                string typeImageUri = string.Format("/UpYourLife;component/images/{0}-icon.png", place.Type);
                CustomPushpin placePushpin = new CustomPushpin()
                {         
                    ID = place.PlaceID,
                    IconUri = new Uri(typeImageUri, UriKind.RelativeOrAbsolute),
                    Height = 48,
                    Width = 48,
                    Place = place
                };                

                UcPushpinPlaces ucPushpin = new UcPushpinPlaces()
                {
                    CustomPushpin = placePushpin,
                    Location = new Location(place.Latitude, place.Longitude)
                };                
                
                Pushpin pushpin = new Pushpin();                

                pinLayer.AddChild(ucPushpin);
                pinLayer.AddChild(pushpin, new Location(place.Latitude, place.Longitude), PositionOrigin.Center);

                ucPushpin.MouseLeftButtonDown += new MouseButtonEventHandler(ucPushpin_MouseLeftButtonDown);
            }
        }

        void ucPushpin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UcPushpinPlaces pushpinPlace = sender as UcPushpinPlaces;
            Place place = pushpinPlace.CustomPushpin.Place;
            PlaceDialog dialog = new PlaceDialog(place);
            dialog.Show(DialogStyle.ModalDimmed);
        }       

        void BuildMyLocation()
        {
            UcPulsar ucPulsar = new UcPulsar();
            UcPulsar ucPulsar1 = new UcPulsar();
            Pushpin myLocationPushpin = new Pushpin();
            pinLayer.AddChild(ucPulsar1, myLocation, PositionOrigin.Center);
            pinLayer.AddChild(myLocationPushpin, myLocation);
            
        }        

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            stbHidePopup.Completed += new EventHandler(stbHidePopup_Completed);
            stbHidePopup.Begin();
        }  

        private void btnFindAdvance_Click(object sender, RoutedEventArgs e)
        {
            gridFindAdvance.Visibility = Visibility.Visible;
        }

        public class PlaceDialog: Dialog
        {
            Place place;
            public PlaceDialog(Place place)
            {
                this.place = place;
            }

            protected override FrameworkElement GetContent()
            {
                UcPlaceDialog ucPlaceDialog = new UcPlaceDialog()
                {
                    Place = place
                };
                return ucPlaceDialog;
            }

            protected override void OnClickOutside()
            {
                Close();
            }
        }

        private void btnApplicationBarPlaces_Click(object sender, EventArgs e)
        {
            PopupFindPlaces.Visibility = Visibility.Visible;
            stbShowPopup.Begin();
        }

        private void btnApplicationBarMyLocation_Click(object sender, EventArgs e)
        {
            GoogleMap.Center = new Location(myProfile.Latitude, myProfile.Longitude);
            GoogleMap.ZoomLevel = 15;
            BuildMyLocation();
        }

        private void btnApplicationBarAddress_Click(object sender, EventArgs e)
        {
            spFindAddress.Visibility = Visibility.Visible;
            //stbShowPopup.Begin();
        }

        private void btnFindAddress_Click(object sender, RoutedEventArgs e)
        {
            proxy.GetGoogleLocalResponseDataAsync(txtFindAddress.Text);
            proxy.GetGoogleLocalResponseDataCompleted += new EventHandler<GetGoogleLocalResponseDataCompletedEventArgs>(proxy_GetGoogleLocalResponseDataCompleted);
        }

        void proxy_GetGoogleLocalResponseDataCompleted(object sender, GetGoogleLocalResponseDataCompletedEventArgs e)
        {
            //List<Place> lstPlaces = e.Result;

            //BuildPlacePushpin(lstPlaces);
            //GoogleMap.ZoomLevel = 15;
            //GoogleMap.Center = myLocation;

            GoogleLocalResponseData responseData = e.Result;
            try
            {
                result = responseData.responseData.results.First();
                Location location = new Location(result.lat, result.lng);

                BuildAddressLocation();
                GoogleMap.ZoomLevel = 15;
                GoogleMap.Center = new Location(result.lat, result.lng);
            }
            catch
            {
                
            }

            spFindAddress.Visibility = Visibility.Collapsed;

            //stbHidePopup1.Completed += new EventHandler(stbHidePopup1_Completed);
            //stbHidePopup1.Begin();
        }

        void stbHidePopup1_Completed(object sender, EventArgs e)
        {
            spFindAddress.Visibility = Visibility.Collapsed;
            stbHidePopup1.Completed -= stbHidePopup1_Completed;
        }

        void BuildAddressLocation()
        {
            Location location = new Location(result.lat, result.lng);
            pinLayer.Children.Clear();
            UcPulsarRed ucPulsarRed = new UcPulsarRed();            
            Pushpin myLocationPushpin = new Pushpin();
            pinLayer.AddChild(ucPulsarRed, location, PositionOrigin.Center);
            pinLayer.AddChild(myLocationPushpin, location);
        }        
    }
}